Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Linq;
12 : using Cqrs.Configuration;
13 : using cdmdotnet.Logging;
14 : using Cqrs.Exceptions;
15 : using Cqrs.Snapshots;
16 :
17 : namespace Cqrs.Azure.BlobStorage.Events
18 : {
19 : /// <summary>
20 : /// A factory for getting connection strings and container names for <see cref="ISnapshotStore"/> access.
21 : /// This factory supports reading and writing from separate storage accounts. Specifically you can have as many different storage accounts as you want to configure when writing.
22 : /// This allows for manual mirroring of data while reading from the fastest/closest location possible.
23 : /// </summary>
24 : public class TableStorageSnapshotStoreConnectionStringFactory
25 : : TableStorageEventStoreConnectionStringFactory
26 : , ITableStorageSnapshotStoreConnectionStringFactory
27 1 : {
28 : /// <summary>
29 : /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the connection string of the readable storage account if using a separate storage account for reads and writes.
30 : /// </summary>
31 : public static string TableStorageReadableSnapshotStoreConnectionStringKey = "Cqrs.Azure.TableStorage.SnapshotStore.Read.ConnectionStringName";
32 :
33 : /// <summary>
34 : /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the connection string of the writeable storage account if using a separate storage account for reads and writes.
35 : /// This value gets appended with a ".1", ".2" etc allowing you to write to as many different locations as possible.
36 : /// </summary>
37 : public static string TableStorageWritableSnapshotStoreConnectionStringKey = "Cqrs.Azure.TableStorage.SnapshotStore.Write.ConnectionStringName";
38 :
39 : /// <summary>
40 : /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the connection string if using a single storage account for both reads and writes.
41 : /// </summary>
42 : public static string TableStorageSnapshotStoreConnectionStringKey = "Cqrs.Azure.TableStorage.SnapshotStore.ConnectionStringName";
43 :
44 : /// <summary>
45 : /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the base name of the container used.
46 : /// </summary>
47 : public static string TableStorageSnapshotBaseContainerNameKey = "Cqrs.Azure.TableStorage.SnapshotStore.BaseContainerName";
48 :
49 : /// <summary>
50 : /// Instantiates a new instance of <see cref="TableStorageSnapshotStoreConnectionStringFactory"/>.
51 : /// </summary>
52 1 : public TableStorageSnapshotStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
53 : : base(configurationManager, logger)
54 : {
55 : }
56 :
57 : /// <summary>
58 : /// Gets all writeable connection strings. If using a single storage account, then <see cref="TableStorageSnapshotStoreConnectionStringKey"/> will most likely be returned.
59 : /// If a value for <see cref="TableStorageWritableSnapshotStoreConnectionStringKey"/> is found, it will append ".1", ".2" etc returning any additionally found connection string values in <see cref="ConfigurationManager"/>.
60 : /// </summary>
61 1 : public override IEnumerable<string> GetWritableConnectionStrings()
62 : {
63 : Logger.LogDebug("Getting table storage writeable connection strings", "TableStorageSnapshotStoreConnectionStringFactory\\GetWritableConnectionStrings");
64 : try
65 : {
66 : var collection = new List<string> ();
67 :
68 : string tableStorageWritableSnapshotStoreConnectionString = ConfigurationManager.GetSetting(TableStorageWritableSnapshotStoreConnectionStringKey);
69 : if (string.IsNullOrWhiteSpace(tableStorageWritableSnapshotStoreConnectionString))
70 : {
71 : Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", TableStorageWritableSnapshotStoreConnectionStringKey), "TableStorageSnapshotStoreConnectionStringFactory\\GetWritableConnectionStrings");
72 : tableStorageWritableSnapshotStoreConnectionString = ConfigurationManager.GetSetting(TableStorageSnapshotStoreConnectionStringKey);
73 : }
74 :
75 : int writeIndex = 1;
76 : while (!string.IsNullOrWhiteSpace(tableStorageWritableSnapshotStoreConnectionString))
77 : {
78 : collection.Add(tableStorageWritableSnapshotStoreConnectionString);
79 :
80 : tableStorageWritableSnapshotStoreConnectionString = ConfigurationManager.GetSetting(string.Format("{0}.{1}", TableStorageWritableSnapshotStoreConnectionStringKey, writeIndex));
81 :
82 : writeIndex++;
83 : }
84 :
85 : if (!collection.Any())
86 : throw new NullReferenceException();
87 :
88 : return collection;
89 : }
90 : catch (NullReferenceException exception)
91 : {
92 : throw new MissingApplicationSettingException(TableStorageSnapshotStoreConnectionStringKey, string.Format("No application settings named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageSnapshotStoreConnectionStringKey), exception);
93 : }
94 : finally
95 : {
96 : Logger.LogDebug("Getting table storage writeable connection string... Done", "TableStorageSnapshotStoreConnectionStringFactory\\GetWritableConnectionStrings");
97 : }
98 : }
99 :
100 : /// <summary>
101 : /// Gets the readable connection string. If using a single storage account, then <see cref="TableStorageSnapshotStoreConnectionStringKey"/> will most likely be returned.
102 : /// If a value for <see cref="TableStorageReadableSnapshotStoreConnectionStringKey"/> is found, that will be returned instead.
103 : /// </summary>
104 1 : public override string GetReadableConnectionString()
105 : {
106 : Logger.LogDebug("Getting table storage readable connection strings", "TableStorageSnapshotStoreConnectionStringFactory\\GetReadableConnectionStrings");
107 : try
108 : {
109 : string tableStorageWritableSnapshotStoreConnectionString = ConfigurationManager.GetSetting(TableStorageReadableSnapshotStoreConnectionStringKey);
110 : if (string.IsNullOrWhiteSpace(tableStorageWritableSnapshotStoreConnectionString))
111 : {
112 : Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", TableStorageReadableSnapshotStoreConnectionStringKey), "TableStorageSnapshotStoreConnectionStringFactory\\GetReadableConnectionStrings");
113 : tableStorageWritableSnapshotStoreConnectionString = ConfigurationManager.GetSetting(TableStorageSnapshotStoreConnectionStringKey);
114 : }
115 :
116 : if (string.IsNullOrWhiteSpace(tableStorageWritableSnapshotStoreConnectionString))
117 : throw new NullReferenceException();
118 :
119 : return tableStorageWritableSnapshotStoreConnectionString;
120 : }
121 : catch (NullReferenceException exception)
122 : {
123 : throw new MissingApplicationSettingException(TableStorageSnapshotStoreConnectionStringKey, string.Format("No application settings named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageSnapshotStoreConnectionStringKey), exception);
124 : }
125 : finally
126 : {
127 : Logger.LogDebug("Getting table storage readable connection string... Done", "TableStorageSnapshotStoreConnectionStringFactory\\GetReadableConnectionStrings");
128 : }
129 : }
130 :
131 : /// <summary>
132 : /// Returns the name of the base contain to be used.
133 : /// This will be the value from <see cref="ConfigurationManager"/> keyed <see cref="TableStorageSnapshotBaseContainerNameKey"/>.
134 : /// </summary>
135 1 : public override string GetBaseContainerName()
136 : {
137 : Logger.LogDebug("Getting table storage base container name", "TableStorageSnapshotStoreConnectionStringFactory\\GetBaseContainerName");
138 : try
139 : {
140 : string result = ConfigurationManager.GetSetting(TableStorageSnapshotBaseContainerNameKey);
141 :
142 : if (string.IsNullOrWhiteSpace(result))
143 : throw new NullReferenceException();
144 :
145 : return result;
146 : }
147 : catch (NullReferenceException exception)
148 : {
149 : throw new MissingApplicationSettingException(TableStorageSnapshotBaseContainerNameKey, exception);
150 : }
151 : finally
152 : {
153 : Logger.LogDebug("Getting table storage base container name... Done", "TableStorageSnapshotStoreConnectionStringFactory\\GetBaseContainerName");
154 : }
155 : }
156 : }
157 : }
|